/**
 * 
 */
package com.jeecms.wicool.action.shops;

import static com.jeecms.common.page.SimplePage.cpn;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.shops.Coupon;
import com.jeecms.wicool.entity.shops.RedPacket;
import com.jeecms.wicool.entity.shops.ShopInfo;
import com.jeecms.wicool.manager.shops.CouponMng;
import com.jeecms.wicool.manager.shops.RedPacketMng;
import com.jeecms.wicool.manager.shops.WiShopInfoMng;

/**
 * @author spring
 *
 */
@Controller
public class RedPacketAct {
	
	@Autowired
	private RedPacketMng redPacketManager;
	
	@Autowired
	private CouponMng couponMng;
	
	@Autowired
	private WiShopInfoMng shopMng;
	
	@RequestMapping("/redPacket/v_list.do")
	public String list(String name,String startDate,String endDate,Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = redPacketManager.getPage(name,startDate,endDate, cpn(pageNo), CookieUtils.getPageSize(request),CmsUtils.getUser(request));
		model.addAttribute("pagination", pagination);
		model.addAttribute("redPacketName", name);
		model.addAttribute("beginDate",startDate);
		model.addAttribute("endDate",endDate);
		return "shops/list_RedPacket";
	}
	
	@RequestMapping("/redPacket/v_add.do")
	public String add(ModelMap model,HttpServletRequest request) {
		RedPacket redPacket = new RedPacket();
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		List<Coupon> listCoupon = couponMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("redPacket", redPacket);
		model.addAttribute("listShop", listShop);
		model.addAttribute("listCoupon", listCoupon);
		return "shops/add_RedPacket";
	}

	@RequestMapping("/redPacket/v_save.do")
	public String save(RedPacket redPacket,Integer shopId,Integer couponId,HttpServletRequest request, ModelMap model){
		WebErrors errors = validate(request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		redPacket.setCoupon(new Coupon());
		redPacket.getCoupon().setId(couponId);
		redPacketManager.save(redPacket,shopId);
		return "redirect:v_list.do";
	}
	
	@RequestMapping("/redPacket/v_edit.do")
	public String edit(Integer id,HttpServletRequest request, ModelMap model){
		WebErrors errors =  validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		List<ShopInfo> listShop= shopMng.getAll(CmsUtils.getUser(request));
		RedPacket entity = redPacketManager.findById(id);
		List<Coupon> listCoupon = couponMng.getAll(CmsUtils.getUser(request));
		model.addAttribute("redPacket", entity);
		model.addAttribute("shopId",entity.getShop().getId());
		model.addAttribute("couponId",entity.getCoupon().getId());
		model.addAttribute("listCoupon", listCoupon);
		model.addAttribute("listShop", listShop);
		return "shops/edit_RedPacket";
	}
	
	@RequestMapping("/redPacket/v_update.do")
	public String update(RedPacket redPacket,Integer shopId,Integer couponId,String name,String startDate,String endDate,Integer pageNo, HttpServletRequest request, ModelMap model){
		WebErrors errors =  validateEdit(redPacket.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		redPacket.setShop(new ShopInfo());
		redPacket.getShop().setId(shopId);
		redPacket.setCoupon(new Coupon());
		redPacket.getCoupon().setId(couponId);
		redPacket = redPacketManager.update(redPacket);
		return list(name, startDate, endDate, pageNo, request, model);
	}
	
	@RequestMapping("/redPacket/v_delete.do")
	public String delete(Integer[] ids,String name,String startDate,String endDate,Integer pageNo, HttpServletRequest request, ModelMap model){
		WebErrors errors =validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		redPacketManager.deleteByIds(ids);
		return list(name, startDate, endDate, pageNo, request, model);
	}
	
	private WebErrors validate(HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}
	
	private WebErrors validateEdit(Integer id,HttpServletRequest request){
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}
	
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}
	
	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		RedPacket entity = redPacketManager.findById(id);
		if (errors.ifNotExist(entity, RedPacket.class, id)) {
			return true;
		}
		return false;
	}
}
